package com.sy.core.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sy.core.model.SyBanner;
import com.sy.core.util.RestResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("banner")
public class BannerCtl extends BaseCtl{

    @RequestMapping("index")
    public String index(){
        return "back/banner/index";
    }

    @RequestMapping("list")
    @ResponseBody
    public RestResult list(Integer page, Integer limit, SyBanner syBanner){
        RestResult rr =null;
        PageHelper.startPage(page,limit);
        try {
            List<Map<String, Object>> bannerList = bannerService.queryList(syBanner);
            PageInfo<Map<String,Object>> info = new PageInfo<>(bannerList);
            rr = new RestResult("200","获取banner列表数据成功",info);
        } catch (Exception e) {
            logger.error("获取banner列表数据失败");
            rr = new RestResult("500","获取banner列表数据失败");
        }
        return  rr;
    }

    @RequestMapping("add")
    public String add(){
        return "back/banner/bannerEdit";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public RestResult saveOrUpdate(SyBanner syBanner){
        RestResult rr = null;
        try {
            bannerService.insertSelective(syBanner);
            rr = new RestResult("200","保存成功");
        } catch (Exception e) {
            logger.error("保存失败");
            rr = new RestResult("500","保存失败");
        }
        return rr;
    }

    @RequestMapping("update")
    public String update(String pk, ModelMap mv){
        Map<String,Object> bannerMap = bannerService.queryOne(pk);
        mv.addAttribute("mv",bannerMap);
        return "back/banner/bannerEdit";
    }

    @RequestMapping("del")
    @ResponseBody
    public RestResult del(String pks){
        RestResult rr = null;
        try {
            bannerService.batchDel(pks);
            rr = new RestResult("200","删除成功");
        } catch (Exception e) {
            logger.error("删除失败");
            rr = new RestResult("500","删除失败");
        }
        return rr;
    }
}
